<template>
  <el-dialog title="新增设备" :visible="showDialog" @close="btnCancel">
    <el-form ref="equipmentForm" :model="formData" :rules="rules">
      <el-form-item label="设备编号：">
        <span>系统自动生成</span>
      </el-form-item>
      <el-form-item label="选择型号：" prop="nodeId">
        <el-select v-model="formData.vmType" placeholder="请选择">
          <el-option v-for="(value,index) in typeList" :key="index" :label="value.name" :value="value.typeId" />
        </el-select>
      </el-form-item>
      <el-form-item label="选择点位：" prop="vmType">
        <el-select v-model="formData.nodeId" placeholder="请选择">
          <el-option v-for="(value,index) in orderList" :key="index" :label="value.name" :value="value.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button @click="btnCancel">取消</el-button>
          <el-button type="warning" @click="btnOK">确认</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addEquipment } from '@/api/equipment'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    typeList: {
      type: Array,
      required: true
    },
    orderList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      page: {
        pageIndex: 1,
        pageSize: 100000
      },
      formData: {
        createUserId: 1,
        nodeId: '',
        vmType: ''
      },
      rules: {
        nodeId: [{ required: true, message: '请输入', trigger: 'blur' }],
        vmType: [{ required: true, message: '请输入', trigger: 'blur' }]
      }

    }
  },
  created() {
  },
  methods: {
    async btnOK() {
      try {
        await this.$refs.equipmentForm.validate()
        await addEquipment(this.formData)
        this.$emit('update:showDialog', false)
        this.$emit('addEquipment')
        this.$message.success('添加成功')
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      this.formData = {
        createUserId: 1,
        nodeId: '',
        vmType: ''
      }
      this.$emit('update:showDialog', false)
      this.$refs.equipmentForm.resetFields()
    }
  }
}
</script>

<style lang="scss">
.el-form-item{
  margin-bottom: 0;
}
.el-select-dropdown__item.selected{
  color: #606266;
  font-weight: normal;
}
.el-form-item__error{
  top: 77%;
  left: 126px;
}
</style>
